home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / doc / pod / GUSI_Files.pod < prev    next >
Encoding:
Text File  |  1995-04-07  |  9.9 KB  |  338 lines  |  [TEXT/CWIE]

  1. =head1 File system calls
  2.  
  3. Files are unlike sockets in many respects: Their length is
  4. never changed by other processes, they can be rewound. There are also many calls which
  5. are specific to files.
  6.  
  7. =head2 Differences to generic behavior
  8.  
  9.  
  10. The following calls make no sense for files and return an error of C<EOPNOTSUPP>:
  11.  
  12.     socket()
  13.     bind()
  14.     listen()
  15.     accept()
  16.     connect()
  17.     getsockname()
  18.     getpeername()
  19.     getsockopt()
  20.     setsockopt()
  21.  
  22. The following calls I<will> work, but might be frowned upon by your friends
  23. (besides, UNIX systems generally wouldn't like them):
  24.  
  25.     recv()
  26.     recvfrom()
  27.     recvmsg()
  28.     send()
  29.     sendto()
  30.     sendmsg()
  31.  
  32. C<choose()> returns zero terminated C strings in C<name>.
  33. It accepts an additional flag C<CHOOSE_DIR>. If this is set, C<choose()>
  34. will select directories instead of files.
  35.  
  36. You may restrict the files presented for choosing by passing a pointer to
  37. the following structure for the C<constraint> argument:
  38.  
  39.     typedef struct {
  40.       short       numTypes;   /* Number of legitimate file types */
  41.       SFTypeList  types;      /* The types, like 'TEXT' */
  42.     } sa_constr_file;
  43.  
  44. C<select()> will give boring results. File descriptors are I<always> considered
  45. ready to read or write, and I<never> give exceptions.
  46.  
  47. C<ioctl()> and C<fcntl()> don't support manipulating the blocking state of a file
  48. descriptor or reading the number of bytes available for reading, but will accept
  49. lots of other requests---Check with your trusty MPW C documentation.
  50.  
  51. =head2 Routines specific to the file system
  52.  
  53. In this section, you'll meet lots of good
  54. old friends. Some of these routines also exist in the standard MPW libraries, but
  55. the C<GUSI> versions have a few differences:
  56.  
  57. =over 4
  58.  
  59. =item *
  60.  
  61. File names are relative to the directory specified by chdir().
  62.  
  63. =item *
  64.  
  65. You can define special treatment for some file names (See below under 
  66. "Adding your own file families").
  67.  
  68. =item *
  69.  
  70. You can pass C<FSSpec> values to the routines by encoding
  71. them with C<FSp2Encoding()> (See "FSSpec routines" below).
  72.  
  73. =back
  74.  
  75. C<int stat(const char * path, struct stat * buf)> returns information about a file.
  76. C<struct stat> is defined as follows:
  77.  
  78.     struct stat   {
  79.       dev_t    st_dev;     /* Volume reference number of file  */
  80.       ino_t    st_ino;     /* File or directory ID             */
  81.       u_short  st_mode;    /* Type and permission of file      */
  82.       short    st_nlink;   /* Always 1             */
  83.       short    st_uid;     /* Set to 0             */
  84.       short    st_gid;     /* Set to 0             */
  85.       dev_t    st_rdev;    /* Set to 0             */
  86.       off_t    st_size;
  87.       time_t   st_atime;   /* Set to st_mtime    */
  88.       time_t   st_mtime;
  89.       time_t   st_ctime;
  90.       long     st_blksize;
  91.       long     st_blocks;
  92.     };
  93.  
  94. C<st_mode> is composed of a file type and of file permissions. The file type
  95. may be one of the following:
  96.  
  97. =over 4
  98.  
  99. =item C<S_IFREG>
  100.  
  101. A regular file.
  102.  
  103. =item C<S_IFDIR>
  104.  
  105. A directory.
  106.  
  107. =item C<S_IFLNK>
  108.  
  109. A finder alias file.
  110.  
  111. =item C<S_IFCHR>
  112.  
  113. A console file under MPW or SIOW.
  114.  
  115. =item C<S_IFSOCK>
  116.  
  117. A file representing a UNIX domain socket.
  118.  
  119. =back
  120.  
  121. Permissions consist of an octal digit repeated three times. The three bits
  122. in the digit have the following meaning:
  123.  
  124. =over 4
  125.  
  126. =item C<4>
  127.  
  128. File can be read.
  129.  
  130. =item C<2>
  131.  
  132. File can be written.
  133.  
  134. =item C<1>
  135.  
  136. File can be executed, i.e., its type is `APPL' or 'appe'. The definition of executability
  137. can be customized with the C<GUSI_ExecHook> discussed in the advanced section.
  138.  
  139. =back
  140.  
  141. C<int lstat(const char * path, struct stat * buf)> works just like C<stat()>, but if C<path>
  142. is a symbolic link, C<lstat()> will return information about the link and not about
  143. the file it points to.
  144.  
  145. C<int fstat(int fd, struct stat * buf)> is the equivalent of C<stat()> for descriptors
  146. representing open files. While it is legal to call C<fstat()> for sockets, the
  147. information returned is not really interesting. The file type in C<st_mode> will be 
  148. C<S_IFSOCK> for sockets.
  149.  
  150. C<int chmod(const char * filename, mode_t mode)> changes the mode returned by C<stat()>. Currently,
  151. the only thing you can do with C<chmod()> is to turn the write permission off an on. This
  152. is translated to setting and clearing the file lock bit.
  153.  
  154. C<int utime(const char * file, const struct utimbuf * tim)> changes the modification time
  155. of a file. C<struct utimbuf> is defined as:
  156.  
  157.     struct utimbuf {
  158.       time_t actime;       /* Access time */
  159.       time_t modtime;      /* Modification time */
  160.     };
  161.  
  162. C<actime> is ignored, as the Macintosh doesn't store access times. The modification
  163. of C<file> is set to C<modtime>.
  164.  
  165. C<int isatty(int fd)> returns C<1> if C<fd> represents a terminal (i.e. is connected
  166. to C<"Dev:Stdin"> and the like), C<0> otherwise.
  167.  
  168. C<long lseek(int, long, int)> works the same as the C<MPW> routine, and will
  169. return C<ESPIPE> if called for a socket.
  170.  
  171. C<int remove(const char *filename)> removes the named file. If C<filename> is a
  172. symbolic link, the link will be removed and not the file.
  173.  
  174. C<int unlink(const char *filename)> is identical to C<remove()>. Note that
  175. on the Mac, C<unlink()> on open files behaves differently from C<UNIX>.
  176.  
  177. C<int rename(const char *oldname, const char *newname)> renames and/or moves a
  178. file. C<oldname> and C<newname> must specify the same volume, but as opposed to
  179. the standard C<MPW> routine, they may specify different folders.
  180.  
  181. C<int open(const char*, int flags)> opens a named file. The C<flags> consist
  182. of one of the following modes:
  183.  
  184. =over 4
  185.  
  186. =item C<O_RDONLY>
  187.  
  188. Open for reading only.
  189.  
  190. =item C<WR_ONLY>
  191.  
  192. Open for writing only.
  193.  
  194. =item C<O_RDWR>
  195.  
  196. Open for reading and writing.
  197.  
  198. =back
  199.  
  200. Optionally combined with one or more of:
  201.  
  202. =over 4
  203.  
  204. =item C<O_APPEND>
  205.  
  206. The file pointer is set to the end of the file before each write.
  207.  
  208. =item C<O_RSRC>
  209.  
  210. Open resource fork.
  211.  
  212. =item C<O_CREAT>
  213.  
  214. If the file does not exist, it is created.
  215.  
  216. =item C<O_EXCL>
  217.  
  218. In combination with C<O_CREAT>, return an error if file already exists.
  219.  
  220. =item C<O_TRUNC>
  221.  
  222. If the file exists, its length is truncated to 0; the mode is unchanged.
  223.  
  224. =item C<O_ALIAS>
  225.  
  226. If the named file is a symbolic link, open the link, not the file it points
  227. to (This is most likely an incredibly bad idea).
  228.  
  229. =back
  230.  
  231. C<int creat(const char * name)> is identical to C<open(name, O_WRONLY+O_TRUNC+O_CREAT)>.
  232. If the file didn't exist before, C<GUSI> determines its file type and creator of the 
  233. according to rules outlined in the section "Resources" below.
  234.  
  235. C<int faccess(const char *filename, unsigned int cmd, long *arg)> works the same
  236. as the corresponding C<MPW> routine, but respects calls to C<chdir()> for partial
  237. filenames.
  238.  
  239. C<void fgetfileinfo(char *filename, unsigned long *newcreator, unsigned long *newtype)>
  240. returns the file type and creator of a file.
  241.  
  242. C<void fsetfileinfo(char *filename, unsigned long newcreator, unsigned long newtype)>
  243. sets the file type and creator of a file to the given values.
  244.  
  245. C<int symlink(const char* linkto, const char* linkname)> creates a file named C<linkname> that
  246. contains an alias resource pointing to C<linkto>. The created file should be
  247. indistinguishible from an alias file created by the System 7 Finder. Note that
  248. aliases bear only superficial similiarities to C<UNIX> symbolic links, especially
  249. once you start renaming files.
  250.  
  251. C<int readlink(const char* path, char* buf, int bufsiz)> returns in C<buf> the name of the
  252. file that C<path> points to.
  253.  
  254. C<int truncate(const char * path, off_t length)> causes a file to have a size equal to C<length>
  255. bytes, shortening it or extending it with zero bytes as necessary.
  256.  
  257. C<int ftruncate(int fd, off_t length)> does the same thing with an open file.
  258.  
  259. C<int access(const char * path, int mode)> tests if you have the specified access 
  260. rights to a file. C<mode> may be either C<F_OK>, in which case the file is tested
  261. for existence, or a combination of the following:
  262.  
  263. =over 4
  264.  
  265. =item C<R_OK>
  266.  
  267. Tests if file is readable.
  268.  
  269. =item C<W_OK>
  270.  
  271. Tests if file is writeable.
  272.  
  273. =item C<X_OK>
  274.  
  275. Tests if file is executable. As with C<stat()>, the definition of executability
  276. may be customized.
  277.  
  278. =back
  279.  
  280. C<access()> returns 0 if the specified access rights exist, otherwise it sets 
  281. C<errno> and returns -1.
  282.  
  283. C<int mkdir(const char * path)> creates a new directory.
  284.  
  285. C<int rmdir(const char * path)> deletes an empty directory.
  286.  
  287. C<int chdir(const char * path)> makes all future partial pathnames relative to this
  288. directory.
  289.  
  290. C<char * getcwd(const char * buf, int size)> returns a pointer to the current directory
  291. pathname. If C<buf> is C<NULL>, C<size> bytes will be allocated using C<malloc()>.
  292.  
  293. Error codes:
  294.  
  295. =over 4
  296.  
  297. =item C<ENAMETOOLONG>
  298.  
  299. The pathname of the current directory is greater than C<size>.
  300.  
  301. =item C<ENOMEM>
  302.  
  303. C<buf> was C<NULL> and C<malloc()> failed.
  304.  
  305. =back
  306.  
  307. A number of calls facilitate scanning directories. Directory entries are represented by 
  308. following structure:
  309.  
  310.     struct dirent {
  311.       u_long   d_fileno;      /* file number of entry */
  312.       u_short  d_reclen;      /* length of this record */
  313.       u_short  d_namlen;      /* length of string in d_name */
  314.     #define MAXNAMLEN   255
  315.       char  d_name[MAXNAMLEN + 1];  /* name must be no longer than this */
  316.     };
  317.  
  318. C<DIR * opendir(const char * dirname)> opens a directory stream and returns a pointer or C<NULL>
  319. if the call failed.
  320.  
  321. C<struct dirent * readdir(DIR * dirp)> returns the next entry from the directory or C<NULL>
  322. if all entries have been processed.
  323.  
  324. C<long telldir(const DIR * dirp)> returns the position in the directory.
  325.  
  326. C<void seekdir(DIR * dirp, long loc)> changes the position.
  327.  
  328. C<void rewinddir(DIR * dirp)> restarts a scan at the beginning.
  329.  
  330. C<int closedir(DIR * dirp)> closes the directory stream.
  331.  
  332. C<int scandir(const char * path, struct dirent *** entries, int (*want)(struct dirent *), int (*sort)(const void *, const void *))> 
  333. scans a whole directory at once and returns
  334. a possibly sorted list of entries. If C<want> is not C<NULL>, only entries for which C<want>
  335. returns 1 are returned. If C<sort> is not C<NULL>, the list is sorted using C<qsort()> with
  336. sort as a comparison function. If sort is C<NULL>, the list will be sorted alphabetically on
  337. a Mac, but not necessarily on other machines.
  338.